﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Windows.Forms;

namespace ClipboardEx
{
    public class appconfig : ConfigurationSection
    {
        public enum EConfigTxt
        {
            CONFIGSTR_M1,
            CONFIGSTR_MC1,
            CONFIGSTR_M2,
            CONFIGSTR_MC2,
            CONFIGSTR_K1,
            CONFIGSTR_K2,
            CONFIGSTR_MAX,
        }

        public enum EControlType
        {
            Del,
            Copy,
        }
        #region 设置变量
        public bool StartWithWin
        {
            get;
            set;
        }

        public bool CloseForMin
        {
            get;
            set;
        }

        public int MaxRecord
        {
            get;
            set;
        }

        public bool PopWhenClipChange
        {
            get;
            set;
        }

        public bool UseHotKey
        {
            get;
            set;
        }

        string[] mConfigText = new string[(int)EConfigTxt.CONFIGSTR_MAX];
        #endregion

        public appconfig()
        {
            SetDefatult();
        }

        public string GetConfig(EConfigTxt ec)
        {
            if (ec == EConfigTxt.CONFIGSTR_MAX)
            {
                return "";
            }

            return mConfigText[(int)ec];
        }

        public bool SetConfig(EConfigTxt ec, string v)
        {
            if (ec == EConfigTxt.CONFIGSTR_MAX)
            {
                return false;
            }

            mConfigText[(int)ec] = v;
            return true;
        }

        public bool IsControlChecked(MouseButtons b, int clicks, EControlType c)
        {
            bool ret = false;
            switch (b)
            {
                case MouseButtons.Left:
                    if (clicks == 1)
                    {
                        if (mConfigText[(int)EConfigTxt.CONFIGSTR_M1] == "LButton")
                        {
                            ret = mConfigText[(int)EConfigTxt.CONFIGSTR_MC1] == c.ToString();
                        }
                    }
                    else
                    {
                        if (mConfigText[(int)EConfigTxt.CONFIGSTR_M1] == "LDButton")
                        {
                            ret = mConfigText[(int)EConfigTxt.CONFIGSTR_MC1] == c.ToString();
                        }
                    }
                    break;
                case MouseButtons.Right:
                    if (clicks == 1)
                    {
                        if (mConfigText[(int)EConfigTxt.CONFIGSTR_M2] == "RButton")
                        {
                            ret = mConfigText[(int)EConfigTxt.CONFIGSTR_MC2] == c.ToString();
                        }
                    }
                    else
                    {
                        if (mConfigText[(int)EConfigTxt.CONFIGSTR_M2] == "RDButton")
                        {
                            ret = mConfigText[(int)EConfigTxt.CONFIGSTR_MC2] == c.ToString();
                        }
                    }
                    break;
                default:
                    break;
            }

            return ret;
        }

        public void SetControlChecked(MouseButtons b, int clicks, EControlType c)
        {
            switch (b)
            {
                case MouseButtons.Left:
                    if (clicks == 1)
                    {
                        mConfigText[(int)EConfigTxt.CONFIGSTR_M1] = "LButton";
                        mConfigText[(int)EConfigTxt.CONFIGSTR_MC1] = c.ToString();
                    }
                    else
                    {
                        mConfigText[(int)EConfigTxt.CONFIGSTR_M1] = "LDButton";
                        mConfigText[(int)EConfigTxt.CONFIGSTR_MC1] = c.ToString();
                    }
                    break;
                case MouseButtons.Right:
                    if (clicks == 1)
                    {
                        mConfigText[(int)EConfigTxt.CONFIGSTR_M2] = "RButton";
                        mConfigText[(int)EConfigTxt.CONFIGSTR_MC2] = c.ToString();
                    }
                    else
                    {
                        mConfigText[(int)EConfigTxt.CONFIGSTR_M2] = "RDButton";
                        mConfigText[(int)EConfigTxt.CONFIGSTR_MC2] = c.ToString();
                    }
                    break;
                default:
                    break;
            }
        }

        public void SetDefatult()
        {
            StartWithWin = false;
            CloseForMin = true;
            MaxRecord = 100;
            PopWhenClipChange = true;
            UseHotKey = true;

            int i = 0;
            mConfigText[i++] = "LButton";
            mConfigText[i++] = "Copy";
            mConfigText[i++] = "RButton";
            mConfigText[i++] = "Del";
            mConfigText[i++] = "Alt";
            mConfigText[i++] = "C";
        }

        // 载入配置
        public bool LoadConfig(string filename)
        {
            XmlDocument xml = new XmlDocument();
            try
            {
                xml.Load(filename);
                XmlNode node = xml.SelectSingleNode("configuration/baseset/startwithwin");
                StartWithWin = node.InnerText.ToUpper() == "YES";

                node = xml.SelectSingleNode("configuration/baseset/closeformin");
                CloseForMin = node.InnerText.ToUpper() == "YES";

                node = xml.SelectSingleNode("configuration/baseset/maxrecord");
                MaxRecord = Convert.ToInt32(node.InnerText);

                XmlNodeList nodelist = xml.SelectNodes("configuration/control/mouse");
                int k = 0;
                for (int i = 0; i < nodelist.Count && i < 2; i++)
                {
                    node = nodelist.Item(i);
                    mConfigText[k++] = node.Attributes["type"].Value;
                    mConfigText[k++] = node.Attributes["action"].Value;
                }

                node = xml.SelectSingleNode("configuration/control/popwhenclipchange");
                PopWhenClipChange = node.InnerText.ToUpper() == "YES";

                node = xml.SelectSingleNode("configuration/control/usehotkey");
                UseHotKey = node.Attributes["use"].Value.ToUpper() == "YES";

                node = xml.SelectSingleNode("configuration/control/usehotkey/keypair");

                mConfigText[k++] = node.Attributes["first"].Value;
                mConfigText[k++] = node.Attributes["second"].Value;

                return true;
            }
            catch
            {
            }

            return false;
        }

        //保存配置
        public void SaveConfig(string filename)
        {
            int k = 0;
            XElement xml = new XElement(
                new XElement("configuration",
                    new XElement("baseset",
                        new XElement("startwithwin", StartWithWin ? "Yes" : "No"),
                        new XElement("closeformin", CloseForMin ? "Yes" : "No"),
                        new XElement("maxrecord", MaxRecord.ToString())
                            ),
                    new XElement("control",
                        new XElement("mouse", new XAttribute("type", mConfigText[k++]), new XAttribute("action", mConfigText[k++])),
                        new XElement("mouse", new XAttribute("type", mConfigText[k++]), new XAttribute("action", mConfigText[k++])),
                        new XElement("popwhenclipchange", PopWhenClipChange ? "Yes" : "No"),
                        new XElement("usehotkey", new XAttribute("use", UseHotKey ? "Yes" : "No"),
                            new XElement("keypair", new XAttribute("first", mConfigText[k++]), new XAttribute("second", mConfigText[k++])))
                    )
                    )
                    );

            XmlWriterSettings s = new XmlWriterSettings();
            s.Encoding = new UTF8Encoding(false);
            s.Indent = true;
            XmlWriter xw = XmlWriter.Create(filename, s);
            xml.Save(xw);

            xw.Flush();
            xw.Close();
        }
    }
}
